﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class Product : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                WebPartBind.BindMasterDataDDL(this.ddlRMType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDRMType"].ToString()));
                WebPartBind.BindMasterDataDDL(this.ddlProductType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDProductType"].ToString()));
                BindProductRM();
            }
        }

        #region Private Method

        private void BindEditProductRM()
        {
            RMService rMServiceObj = ServiceFactory.getRMService();
            OperationResult<List<tb_RM>> result = rMServiceObj.GetRMByTypeId(Int32.Parse(ddlRMType.SelectedValue));

            if (result.operationSuccess)
            {
                dlEditRMLeft.DataSource = result.obj;
                dlEditRMLeft.DataTextField = "Name";
                dlEditRMLeft.DataValueField = "Id";
                dlEditRMLeft.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }

        } 

        private void BindProductRM()
        {
            RMService rMServiceObj = ServiceFactory.getRMService();
            OperationResult<List<tb_RM>> result = rMServiceObj.GetRMByTypeId(Int32.Parse(ddlRMType.SelectedValue));

            if (result.operationSuccess)
            {
                dlRMLeft.DataSource = result.obj;
                dlRMLeft.DataTextField = "Name";
                dlRMLeft.DataValueField = "Id";
                dlRMLeft.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }

        }

        private void BindGridView()
        {
            ProductService productServiceObj = ServiceFactory.getProductService();

            OperationResult<List<tb_Product>> result = productServiceObj.GetAllProduct();

            if (result.operationSuccess)
            {
                gvProductList.DataSource = result.obj;
                gvProductList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            WebPartBind.BindMasterDataDDL(this.ddlEditRMType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDRMType"].ToString()));
            WebPartBind.BindMasterDataDDL(this.ddlEditProductType, Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDProductType"].ToString()));
            BindEditProductRM();

            ProductService productServiceObj = ServiceFactory.getProductService();
            OperationResult<tb_Product> resultEdit = productServiceObj.GetProductById(id);
            if (resultEdit.operationSuccess == true)
            {
                txtEditName.Text = resultEdit.obj.Name;
                txtEditDescription.Text = resultEdit.obj.Description;
                ddlEditProductType.SelectedValue = resultEdit.obj.CategoryId.ToString();
                txtEditBasePriceByArea.Text = resultEdit.obj.BasePriceByArea.ToString();
                txtEditSalePriceByArea.Text = resultEdit.obj.SalePriceByArea.ToString();
                txtEditBasePriceByWeight.Text = resultEdit.obj.BasePriceByWeight.ToString();
                txtEditSalePriceByWeight.Text = resultEdit.obj.SalePriceByWeight.ToString(); 
                txtEditDescription.Text = resultEdit.obj.Description;
 
                OperationResult<List<tb_Mapping_RMProduct>> resultMNapping = productServiceObj.GetProductRMMappingByProductId(id);

                foreach (tb_Mapping_RMProduct mappingEntity in resultMNapping.obj)
                {
                    ListItem item = new ListItem();
                    item.Text = mappingEntity.tb_RM.Name;
                    item.Value = mappingEntity.RMId.ToString ();
                    
                    dlEditRMRight.Items.Add(item);
                    dlEditRMLeft.Items.Remove(item);
                }

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();

            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ProductService productServiceObj = ServiceFactory.getProductService();
            tb_Product instance = new tb_Product();

            instance.Name = txtName.Text;
            instance.Description = txtDescription.Text;
            instance.BasePriceByArea = decimal.Parse(txtBoxBasePriceByArea.Text);
            instance.BasePriceByWeight = decimal.Parse(txtBasePriceByWeight.Text);
            instance.CategoryId = Int32.Parse(ddlProductType.SelectedValue);
            instance.SalePriceByArea = decimal.Parse(txtSalePriceByArea.Text);
            instance.SalePriceByWeight = decimal.Parse(txtSalePriceByWeight.Text); 

            OperationResult<tb_Product> result = productServiceObj.CreateProduct(instance);
            if (result.operationSuccess)
            {
                tb_Mapping_RMProduct RMPRODUCTInstance = new tb_Mapping_RMProduct();
                int productId = result.obj.Id;
                foreach (ListItem selectItem in dlRMRight.Items)
                {
                    int selectId = Int32.Parse(selectItem.Value);
                    tb_Mapping_RMProduct selectEntity = new tb_Mapping_RMProduct();
                    selectEntity.ProductId = productId;
                    selectEntity.RMId = selectId;
                    OperationResult<tb_Mapping_RMProduct> mappingResult = productServiceObj.CreateProductMappingWithRM(selectEntity);

                }
            }
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            ProductService productServiceObj = ServiceFactory.getProductService();

            tb_Product entity = new tb_Product();

            entity.Name = txtEditName.Text;
            entity.Description = txtEditDescription.Text;
            entity.BasePriceByArea = decimal.Parse(txtEditBasePriceByArea.Text);
            entity.BasePriceByWeight = decimal.Parse(txtEditBasePriceByWeight.Text);
            entity.CategoryId = Int32.Parse(ddlEditProductType.SelectedValue);
            entity.SalePriceByArea = decimal.Parse(txtEditSalePriceByArea.Text);
            entity.SalePriceByWeight = decimal.Parse(txtEditSalePriceByWeight.Text); 
 
            OperationResult<tb_Product> result = productServiceObj.ModifyProduct(entity);
            if (result.operationSuccess)
            {
                OperationResult<tb_Mapping_RMProduct> resultdeleteRMMapping = productServiceObj.DeleteProductMappingWithRM(entity.Id);

                tb_Mapping_RMProduct RMPRODUCTInstance = new tb_Mapping_RMProduct();
                int productId = result.obj.Id;
                foreach (ListItem selectItem in dlEditRMRight.Items)
                {
                    int selectId = Int32.Parse(selectItem.Value);
                    tb_Mapping_RMProduct selectEntity = new tb_Mapping_RMProduct();
                    selectEntity.ProductId = productId;
                    selectEntity.RMId = selectId;
                    OperationResult<tb_Mapping_RMProduct> mappingResult = productServiceObj.CreateProductMappingWithRM(selectEntity);

                }
            }
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);

            ProductService productServiceObj = ServiceFactory.getProductService();
            tb_Product deleteEntity = new tb_Product();
            deleteEntity.Id = deleteId;

            productServiceObj.DeleteProduct(deleteEntity);
            productServiceObj.DeleteProductMappingWithRM(deleteEntity.Id);

            BindGridView();
        }

        protected void gvProductList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProductList.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void ddlRMType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindProductRM();
        }

        protected void btnLeft_Click(object sender, EventArgs e)
        {
            ListItem iTemLeftSelectItem = dlRMLeft.SelectedItem;
            dlRMRight.Items.Add(iTemLeftSelectItem);
            dlRMLeft.Items.Remove(iTemLeftSelectItem);
            dlRMRight.SelectedIndex = 0;

        }

        protected void btnRight_Click(object sender, EventArgs e)
        {
            ListItem iTemRightSelectItem = dlRMRight.SelectedItem;
            dlRMLeft.Items.Add(iTemRightSelectItem);
            dlRMRight.Items.Remove(iTemRightSelectItem);
        }

        #region for Edit
        protected void ddlEditRMType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindEditProductRM();
        }

        protected void btnEditLeft_Click(object sender, EventArgs e)
        {
            ListItem iTemLeftSelectItem = dlRMLeft.SelectedItem;
            dlEditRMRight.Items.Add(iTemLeftSelectItem);
            dlEditRMLeft.Items.Remove(iTemLeftSelectItem);
            dlEditRMRight.SelectedIndex = 0;

        }

        protected void btnEditRight_Click(object sender, EventArgs e)
        {
            ListItem iTemRightSelectItem = dlEditRMRight.SelectedItem;
            dlEditRMLeft.Items.Add(iTemRightSelectItem);
            dlEditRMRight.Items.Remove(iTemRightSelectItem);
        }
        #endregion
        #endregion


    }
}
